package cn.tedu._05weibo.controller;

import cn.tedu._05weibo.base.utils.CacheUtil;
import cn.tedu._05weibo.common.response.JsonResult;
import cn.tedu._05weibo.common.response.StatusCode;
import cn.tedu._05weibo.mapper.WeiboMapper;
import cn.tedu._05weibo.pojo.dto.WeiboDTO;
import cn.tedu._05weibo.pojo.entity.Weibo;
import cn.tedu._05weibo.pojo.vo.UserVO;
import cn.tedu._05weibo.pojo.vo.WeiboDetailVO;
import cn.tedu._05weibo.pojo.vo.WeiboIndexVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Validated
@Api(tags = "02.微博模块")
@Slf4j
@RestController
@RequestMapping("/v1/weibo/")
public class WeiboController {

    @Autowired
    private WeiboMapper weiboMapper;

    //自己创建的工具类，调用方法更方便
    @Autowired
    private CacheUtil cacheUtil;

    @ApiOperation(value = "发布微博功能")
    @PostMapping("insert")
    public JsonResult insert(@RequestBody WeiboDTO weiboDTO, @ApiIgnore HttpSession session) {
        log.debug("WeiboDTO = " + weiboDTO);
        UserVO userVO = (UserVO) session.getAttribute("user");

        //已在拦截器中进行登录校验

        Weibo weibo = new Weibo();
        BeanUtils.copyProperties(weiboDTO, weibo);
        weibo.setCreated(new Date());
        weibo.setUserId(userVO.getId());

        weiboMapper.insert(weibo);

        return JsonResult.ok();
    }

    @ApiOperation(value = "微博首页功能")
    @GetMapping("selectIndex")
    public JsonResult selectIndex() {

//        ValueOperations<String, Serializable> ops = redisTemplate.opsForValue();
//        Serializable weiboIndex = ops.get("weibo_index");

        Serializable weiboIndex = cacheUtil.get("weibo_index");

        if(weiboIndex == null){
            List<WeiboIndexVO> weiboIndexVOS = weiboMapper.selectIndex();
            System.out.println("首页数据来自MySQL数据库");
//            ops.set("weibo_index", (Serializable) weiboIndexVOS, 30, TimeUnit.SECONDS);
            cacheUtil.setExpire("weibo_index", (Serializable) weiboIndexVOS, 30);

            return JsonResult.ok(weiboIndexVOS);
        }

        System.out.println("首页数据来自Redis数据库");
        return JsonResult.ok(weiboIndex);
    }

    @ApiOperation(value = "微博详情功能")
    @GetMapping("selectById")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id", value = "微博编号", required = true, dataType = "long"),
            @ApiImplicitParam(name = "uersname", value = "用户名", dataType = "string")
    })
    public JsonResult selectById(@Range(min = 1, max = 500, message = "微博ID必须在1-500之间") @RequestParam Long id, String username) {
        if (id < 0) {
            throw new IllegalArgumentException("微博编号不能小于0");
        }
        WeiboDetailVO weiboDetailVO = weiboMapper.selectById(id);
        return JsonResult.ok(weiboDetailVO);
    }
}
